package com.tdam2011.grupo02.ui;

import com.tdam2011.grupo02.R;
import com.tdam2011.grupo02.compatibility.ActionBarActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class PerfilContraseniaActivity extends ActionBarActivity {

	private String contrasenia;
	private EditText contraActualET;
	private EditText contraNueET;
	private EditText contraNueRET;
	private Button aplicar;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.perfil_contrasenia);

		contrasenia = (String) this.getIntent().getExtras().get("contrasenia");
		contraActualET = (EditText) findViewById(R.id.contraseniaAnteriorTxt);
		contraNueET = (EditText) findViewById(R.id.contraseniaNuevaTxt);
		contraNueRET = (EditText) findViewById(R.id.contraseniaRepetirTxt);
		aplicar = (Button) findViewById(R.id.btnContraseniaCambiar);

		aplicar.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				if (contraActualET.getText().toString().equals("")
						|| contraActualET.getText().toString() == null
						|| contraNueET.getText().toString().equals("")
						|| contraNueET.getText().toString() == null
						|| contraNueRET.getText().toString().equals("")
						|| contraNueRET.getText().toString() == null) {
					Toast.makeText(PerfilContraseniaActivity.this,
							getString(R.string.perfilIngreseCamposRequeridos), Toast.LENGTH_LONG)
							.show();
				} else {

					if (contrasenia.equals(contraActualET.getText().toString())) {
						if (contraNueET.getText().toString()
								.equals(contraNueRET.getText().toString())) {
							if (contraNueET.length() > 5) {
								contrasenia = contraNueET.getText().toString();
								Intent i = new Intent();
								i.putExtra("contrasenia", contrasenia);
								setResult(0, i);
								finish();
							} else {
								Toast.makeText(
										PerfilContraseniaActivity.this,
										getString(R.string.condicionMinimaContraseniaPerfil),
										Toast.LENGTH_LONG).show();
							}
						} else {
							Toast.makeText(PerfilContraseniaActivity.this,
									getString(R.string.condicionCoincidirContraseniaPerfil),
									Toast.LENGTH_LONG).show();
							contraNueET.setText("");
							contraNueRET.setText("");
						}
					} else {
						Toast.makeText(PerfilContraseniaActivity.this,
								getString(R.string.condicionContraseniaActualContraseniaPerfil),
								Toast.LENGTH_LONG).show();
						contraActualET.setText("");
					}
				}
			}
		});
	}

}
